package com.glory.hzlionpda.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hzlionpda.R;
import com.glory.hzlionpda.adapter.ByLotJobInputAdapter;
import com.glory.hzlionpda.bean.ByLotJobBean;
import com.glory.hzlionpda.bean.LotBean;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ByLotOrJobInputActivity extends FreeUI_BaseActivity {


    @BindView(R.id.rvInputList)
    RecyclerView rvInputList;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private ByLotJobInputAdapter byLotJobInputAdapter;
    private LotBean lotBean;
    private int where = 1;
    private transient FreeUI_GeneralFragmentDialog tipDialog;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_by_lot_or_job_input;
    }

    @Override
    protected void initView() {
        rvInputList.setLayoutManager(new LinearLayoutManager(ByLotOrJobInputActivity.this));
        rvInputList.setItemAnimator(new DefaultItemAnimator());
        rvInputList.addItemDecoration(new DividerItemDecoration(ByLotOrJobInputActivity.this, 1));

        byLotJobInputAdapter = new ByLotJobInputAdapter(null);
        byLotJobInputAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        byLotJobInputAdapter.isFirstOnly(true);
        rvInputList.setAdapter(byLotJobInputAdapter);
    }


    @Override
    protected void initData() {
        lotBean = (LotBean) getIntent().getSerializableExtra("lotBean");
        if (lotBean == null) {
            showToast("对象为空，请重新请求页面", false);
            ByLotOrJobInputActivity.this.finish();
            return;
        }
        where = getIntent().getIntExtra("where", 1);
        List<ByLotJobBean> inputList = new ArrayList<>();
        if (where == 1) {
            freeUI_titleView.setTitle("ByLot");
            if (lotBean.getPARAMETERGROUPBYLOT_TEMP().size() > 0) {
                inputList.addAll(lotBean.getPARAMETERGROUPBYLOT_TEMP());
            } else {
                for (String lot : lotBean.getJOBLOTLIST()) {
                    for (ByLotJobBean byLotJobBean : lotBean.getPARAMETERGROUPBYLOT()) {
                        ByLotJobBean tempBean = new ByLotJobBean(lot, byLotJobBean.getPARAMETERSEQ(),
                                byLotJobBean.getPARAMETERID(), byLotJobBean.getPARAMETERDESC(), byLotJobBean.getLBLID(),
                                byLotJobBean.getPARAMETERVALUE(), byLotJobBean.getSEQUENCENUMBER());
                        inputList.add(tempBean);
                    }
                }
            }
        } else {
            for (ByLotJobBean byLotJobBean : lotBean.getPARAMETERGROUPBYJOB()) {
                byLotJobBean.setLOTID("ByJob");
                inputList.add(byLotJobBean);
            }
            freeUI_titleView.setTitle("ByJob");
        }
        byLotJobInputAdapter.setNewData(inputList);
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setLeftListener(view -> showBackDialog());
        freeUI_titleView.setRightListener("提交", v -> {
            if (where == 1) {
                lotBean.setPARAMETERGROUPBYLOT_TEMP(byLotJobInputAdapter.getData());
            } else {
                lotBean.setPARAMETERGROUPBYJOB(byLotJobInputAdapter.getData());
            }
            setResult(2101, new Intent().putExtra("lotBean", lotBean));
            ByLotOrJobInputActivity.this.finish();
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void showBackDialog() {
        tipDialog = FreeApi_DialogUtils.getConfirmAndCancelDialog(
                this, "确定不保存直接退出吗？", ByLotOrJobInputActivity.this::finish, () -> {

                });
        tipDialog.show(getSupportFragmentManager(), "DIALOG");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 当按下返回键时所执行的命令
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            showBackDialog();
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
}
